<%@ page import="com.wuming.caiwu.security.SecurityHander" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理角色信息</title>
    <link rel="stylesheet" href="${allServerPath}static/layui/layui/css/layui.css">
    <style>
        .layui-btn-container{
            margin-left: 20px;
        }
    </style>
</head>
<body style="background-color: #f0f2f5;">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>基本演示</legend>
</fieldset>

<div class="layui-btn-container">
    <button type="button" class="layui-btn" lay-demo="getChecked">保存</button>
</div>

<div id="test12" class="demo-tree-more"></div>
<input type="hidden" id="roleId" name="roleId" value="${roleId}"/>
<script src="${allServerPath}static/layui/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script>
    layui.use(["tree", "util"], function(){
        var tree = layui.tree
            ,layer = layui.layer
            ,util = layui.util
            ,$ = layui.$

            //模拟数据
            ,data = [];


        $(document).ready(function () {
            var roleId = $("#roleId").val();
            loadRolePermissions(roleId);
        });

        function loadRolePermissions(roleId) {
            $.ajax({
                type: "get",
                url: "${allServerPath}web/syspermission/getRolePermission/"+ roleId +".htmls",
                dataType: 'JSON',
                data: {
                    "storeId": "<%=SecurityHander.getCurrentUserId()%>"
                },
                error: function () {
                    layer.alert('操作失败', {
                        icon: 2,
                        title: "提示"
                    });
                },
                success: function (ret) {
                    if (ret.success) {
                        var data = ret.data.allMenus;
                        var permissions = ret.data.rolePermissions;
                        var permissionIds = [];
                        for (let i = 0; i < permissions.length; i++) {
                            if(permissions[i].pid == 0){
                                continue;
                            }
                            permissionIds.push(permissions[i].id);
                        }
                        //基本演示
                        tree.render({
                            elem: '#test12'
                            ,data: data
                            ,showCheckbox: true  //是否显示复选框
                            ,id: 'demoId'
                            ,isJump: true //是否允许点击节点时弹出新窗口跳转
                            ,click: function(obj){
                                var data = obj.data;  //获取当前点击的节点数据
                                layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
                            }
                        });
                        tree.setChecked('demoId', permissionIds);
                    } else {

                    }
                }
            })
        }

        function saveRolePermission(roleId,checkedData){
            $.ajax({
                type: "POST",
                url: "${allServerPath}web/syspermission/updateRolePermission.htmls",
                dataType: 'JSON',
                data: {
                    "roleId": roleId,
                    "checkedData": JSON.stringify(checkedData)
                },
                error: function () {
                    layer.alert('操作失败', {
                        icon: 2,
                        title: "提示"
                    });
                },
                success: function (ret) {
                    if (ret.success) {
                        layer.msg('更新权限成功.');
                    } else {

                    }
                }
            })
        }


        //按钮事件
        util.event('lay-demo', {
            getChecked: function(othis){
                var checkedData = tree.getChecked('demoId'); //获取选中节点的数据
                // layer.alert(JSON.stringify(checkedData), {shade:0});
                // console.log(checkedData);
                var roleId = $("#roleId").val();
                saveRolePermission(roleId,checkedData);
            }
        });
    });
</script>

</body>
</html>